{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Petermann Glacier\n",
    "peterPoints = [(-65.51712706815805,82.15009447647783),\n",
    "(-65.51712706815805,78.38406522295739),\n",
    "(-44.55698414800813,78.38406522295739),\n",
    "(-44.55698414800813,82.15009447647783),\n",
    "(-65.51712706815805,82.15009447647783)]\n",
    "\n",
    "#Jakobshavn\n",
    "jacobsPoints = [\n",
    "(-51.85297729818772, 70.78302286892713),\n",
    "(-51.85297729818772, 68.41440064014121),\n",
    "(-46.50519742294812, 68.37754778777803),\n",
    "(-46.50519742294812, 70.78302286892713),\n",
    "(-51.85297729818772, 70.78302286892713) ]\n",
    "\n",
    "#Antarctica\n",
    "#Thwaites Glacier\n",
    "thwaitesPoints = [\n",
    "(-118.103998296609,-73.18220620018596),\n",
    "(-118.103998296609,-76.27563309307305),\n",
    "(-92.64141079610289,-76.27563309307305),\n",
    "(-92.64141079610289,-73.18220620018596),\n",
    "(-118.103998296609,-73.18220620018596) ]\n",
    "#Drygalski\n",
    "\n",
    "drygalskiPoints = [\n",
    "(-62.2933314638862,-64.27767014248812),\n",
    "(-62.2933314638862,-65.3068813457687),\n",
    "(-58.81729523711281,-65.3068813457687),\n",
    "(-58.81729523711281,-64.27767014248812),\n",
    "(-62.2933314638862,-64.27767014248812)]\n",
    "\n",
    "storstrommen = [( 262956, -1317863), (262956, -985707), (625920,-985707), (625920, -1317863), ( 262956, -1317863) ]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import osgeo.ogr as ogr\n",
    "import osgeo.osr as osr\n",
    "\n",
    "import os\n",
    "\n",
    "def ensure_dir(file_path):\n",
    "    directory = os.path.dirname(file_path)\n",
    "    if not os.path.exists(directory):\n",
    "        os.makedirs(directory)\n",
    "\n",
    "def createShapeFile( name, points, proj ):\n",
    "    driver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "\n",
    "    path = \"ShapeFiles/{}_{}.shp\".format( name, proj.shortName )\n",
    "    ensure_dir(path)\n",
    "    \n",
    "    tmp_driver=ogr.GetDriverByName('MEMORY')\n",
    "    tmp_source=tmp_driver.CreateDataSource('memData')\n",
    "    \n",
    "    # create the data source\n",
    "    data_source = driver.CreateDataSource(path)\n",
    "\n",
    "    # create the spatial reference, WGS84\n",
    "    src = osr.SpatialReference()\n",
    "    src.ImportFromProj4(proj.proj4)\n",
    "    \n",
    "    target = osr.SpatialReference()\n",
    "    target.ImportFromProj4(proj.proj4)\n",
    "\n",
    "    targetprj = target\n",
    "    transform = osr.CoordinateTransformation(src, targetprj)\n",
    "\n",
    "    # create the layer\n",
    "    tmp_layer = tmp_source.CreateLayer(\"landice\", src, ogr.wkbPolygon)\n",
    "\n",
    "    feature = ogr.Feature(tmp_layer.GetLayerDefn())\n",
    "    #create the WKT for the feature using Python string formatting\n",
    "    ring = ogr.Geometry(ogr.wkbLinearRing)\n",
    "    \n",
    "    for x,y in points:\n",
    "        ring.AddPoint(x, y)\n",
    "    \n",
    "    poly = ogr.Geometry(ogr.wkbPolygon)\n",
    "    poly.AddGeometry(ring)\n",
    "    \n",
    "    feature.SetGeometry( poly )\n",
    "    # Create the feature in the layer (shapefile)\n",
    "    \n",
    "    outlayer = data_source.CreateLayer(\"landice\", target,geom_type=ogr.wkbPolygon)\n",
    "\n",
    "    # Add an ID field\n",
    "    idField = ogr.FieldDefn(\"id\", ogr.OFTInteger)\n",
    "    outlayer.CreateField(idField)\n",
    "\n",
    "    # Create the feature and set values\n",
    "    transformed = feature.GetGeometryRef()\n",
    "    transformed.Transform(transform)\n",
    "\n",
    "    geom = ogr.CreateGeometryFromWkb(transformed.ExportToWkb())\n",
    "    defn = outlayer.GetLayerDefn()\n",
    "    feat = ogr.Feature(defn)\n",
    "    feat.SetGeometry(geom)\n",
    "    outlayer.CreateFeature(feat)\n",
    "    \n",
    "    # Dereference the feature\n",
    "    feature = None\n",
    "    tmp_layer = None\n",
    "    outlayer = None\n",
    "    data_source = None\n",
    "    tmp_source = None\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from MalardClient.MalardClient import MalardClient\n",
    "from MalardClient.DataSet import DataSet\n",
    "\n",
    "client = MalardClient()\n",
    "\n",
    "greenlandProjection = client.getProjection( DataSet(\"cryotempo\", \"GRIS_BaselineC_Q2\", \"greenland\") )\n",
    "antarcticProjection = client.getProjection( DataSet( \"cryotempo\", \"AntarcticaV2b\", \"antarctic\" ))\n",
    "\n",
    "#createShapeFile( \"jakobshaven\", jacobsPoints, greenlandProjection )\n",
    "#createShapeFile( \"petermann\", peterPoints, greenlandProjection )\n",
    "#createShapeFile( \"thwaites\", thwaitesPoints, antarcticProjection )\n",
    "#createShapeFile( \"drygalski\", drygalskiPoints, antarcticProjection )\n",
    "\n",
    "createShapeFile( \"storstrommen\", storstrommen, greenlandProjection )\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['File deleted successfully [/data/puma1/scratch/v2/malard/export/cryotempo_GRIS_BaselineC_Q2_-130626864.nc]']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from MalardClient.MalardClient import MalardClient\n",
    "from MalardClient.DataSet import DataSet\n",
    "from MalardClient.MaskFilter import MaskFilter\n",
    "\n",
    "from datetime import datetime\n",
    "\n",
    "client = MalardClient()\n",
    "\n",
    "greenland_ds = DataSet(\"cryotempo\", \"GRIS_BaselineC_Q2\", \"greenland\")\n",
    "greenland_oib = DataSet(\"cryotempo\", \"oib\", \"greenland\")\n",
    "\n",
    "shapeFile = \"/home/jon/IdeaProjects/malard/python/cryotempo/ShapeFiles/storstrommen_NPS.shp\"\n",
    "extentFilter=MaskFilter(p_shapeFile = shapeFile)\n",
    "\n",
    "minT = datetime(2011,3,1,0,0)\n",
    "maxT = datetime(2011,4,1,0,0)\n",
    "\n",
    "#optional filters: Greenland columns below. \n",
    "#'lon', 'lat', 'elev', 'heading', 'demDiff', 'demDiffMad', 'demDiffMad2','phaseAmb', 'meanDiffSpread', 'wf_number', 'sampleNb', 'power',\n",
    "#'powerdB', 'phase', 'phaseS', 'phaseSSegment', 'phaseConfidence', 'coh',\n",
    "#'x', 'y', 'time', 'swathFileId', 'rowId\n",
    "\n",
    "filters = [{\"column\":\"coh\",\"op\":\"gte\",\"threshold\":0.6},{\"column\":\"power\",\"op\":\"gt\",\"threshold\":10000.0}]\n",
    "\n",
    "info = client.executeQueryPolygon( greenland_ds, minT, maxT, extentFilter=extentFilter, filters = filters )\n",
    "\n",
    "infoDf = info.to_df\n",
    "\n",
    "client.releaseCacheHandle(info.resultFileName)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def simplePlot(df):\n",
    "## plot with no filters\n",
    "    fig = plt.figure(figsize=(12,10))\n",
    "    plt.scatter(x=df['x'], y=df['y'], c=df['elev'], marker='.', s=20, vmax=7500)\n",
    "    plt.colorbar()\n",
    "    plt.show()\n",
    "    return fig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "simplePlot( infoDf )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print( len(infoDf) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
